package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

//本类是用来测试JDBC插入数据的演示demo
public class TestInsert {
    public static void main(String[] args) throws Exception {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取和数据库的链接
        //2.1指定URL 确定要链接的是那个数据库
        String url = "jdbc:mysql://localhost:3306/tedu?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        //2.2指定用户名和密码
        String user = "root";
        String psd = "root";
        //2.3调用DriverManager的getConnection方法获取链接
        Connection conn = DriverManager.getConnection(url, user, psd);

        //3.定义sql语句
        String sql = "INSERT INTO location(id,name) VALUES(null,'神之国度')";

        //4.获取执行sql语句的对象
        PreparedStatement ps = conn.prepareStatement(sql);

        //5.通过PreparedStatement执行sql语句
        int rows = ps.executeUpdate();

        //6.处理结果
        System.out.println(rows > 0 ? "添加"+ rows + "行成功" : "添加失败");
        //7.释放资源
        ps.close();
        conn.close();
    }
}
